import os.path

from devicetest.api import Asserts
from devicetest.aw.OpenHarmony import CommonOH
from devicetest.core.test_case import TestCase, Step
from devicetest.log.logger import print_info

from aw.common import common_setup, common_teardown, crop_picture, compare_image_similarity
from aw.pom import Music, DeskTop


class PreTest_RK3568_Multimedia_Music_0500(TestCase, Music):
    # 预置条件：
    # 烧写目标版本后，正常进入桌面
    # 测试步骤：
    # 1.点击音乐应用
    # 2.播放的音乐点击back键后再进入
    # 3.播放的音乐点击home键后再进入
    # 4.播放的音乐点击recent键再进入
    # 预期结果：
    # 1.正常进入音乐应用内
    # 2.点击back后再进入音乐会暂停
    # 3.点击home键后再进入音乐不会暂停
    # 4.点击recent键后再进入音乐不会暂停

    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)
        self.screenshot_dir = os.path.join(self.cur_case.case_screenshot_dir, self.cur_case.name)
        self.resource_dir = os.path.join(os.path.dirname(self.configs['testcases_path']), 'resource')

        self.standard_pause_pic = os.path.join(self.resource_dir, 'music', 'pause.jpeg')
        self.standard_running_pic = os.path.join(self.resource_dir, 'music', 'running.jpeg')

    def setup(self):
        common_setup(self.Phone1)

    def process(self):
        Step('步骤1：点击音乐应用')
        CommonOH.startAbility(self.Phone1, self.ability_name, self.bundle_name)
        Step('预期结果1.正常进入音乐应用内')
        CommonOH.wait(self.Phone1, 2)
        try:
            CommonOH.touchByText(self.Phone1, '允许')
        except:
            CommonOH.click(self.Phone1, **self.allow_permission)
        CommonOH.checkIfKeyExist(self.Phone1, 'image3')

        Step('步骤2.播放的音乐点击back键后再进入')
        # 先点击播放
        CommonOH.touchByKey(self.Phone1, 'image3')
        CommonOH.wait(self.Phone1, 4)
        CommonOH.click(self.Phone1, **DeskTop.back_button)
        Step('预期结果2.点击back后再进入音乐会暂停')
        CommonOH.startAbility(self.Phone1, self.ability_name, self.bundle_name)
        pause_pic = os.path.join(self.screenshot_dir, '{}_pause0.jpeg'.format(self.Phone1.device_sn))
        CommonOH.takePicture(self.Phone1, pause_pic)
        crop_picture(pause_pic, **self.image3_bounds)
        similarity = compare_image_similarity(pause_pic, self.standard_pause_pic)
        print_info('预期结果2.相似度为：{}'.format(similarity))
        Asserts.assert_greater(similarity, 0.5)

        Step('步骤3.播放的音乐点击home键后再进入')
        CommonOH.click(self.Phone1, **self.progress_start)
        CommonOH.touchByKey(self.Phone1, 'image3')
        CommonOH.wait(self.Phone1, 4)
        CommonOH.click(self.Phone1, **DeskTop.home_button)
        Step('预期结果3.点击home键后再进入音乐不会暂停')
        CommonOH.startAbility(self.Phone1, self.ability_name, self.bundle_name)
        running_pic = os.path.join(self.screenshot_dir, '{}_running0.jpeg'.format(self.Phone1.device_sn))
        CommonOH.takePicture(self.Phone1, running_pic)
        crop_picture(running_pic, **self.image3_bounds)
        similarity = compare_image_similarity(running_pic, self.standard_running_pic)
        print_info('预期结果3.相似度为：{}'.format(similarity))
        Asserts.assert_greater(similarity, 0.5)

        Step('步骤4.播放的音乐点击recent键再进入')
        # 直接拖到进度条最后
        CommonOH.click(self.Phone1, **self.progress_start)
        CommonOH.wait(self.Phone1, 5)
        CommonOH.click(self.Phone1, **DeskTop.recent_button)
        CommonOH.click(self.Phone1, **DeskTop.center)
        Step('预期结果4.点击recent键后再进入音乐不会暂停')
        running_pic = os.path.join(self.screenshot_dir, '{}_running1.jpeg'.format(self.Phone1.device_sn))
        CommonOH.takePicture(self.Phone1, running_pic)
        crop_picture(running_pic, **self.image3_bounds)
        similarity = compare_image_similarity(running_pic, self.standard_running_pic)
        print_info('预期结果4.相似度为：{}'.format(similarity))
        Asserts.assert_greater(similarity, 0.5)

    def teardown(self):
        common_teardown(self.Phone1, self.bundle_name)
